package com.circle.interests.biz.core.category.honglian;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.circle.common.service.api.dto.HongLianOrderDto;
import com.circle.common.service.api.enums.EnumHongLianVirtualPrizeType;
import com.circle.common.service.api.service.DubboHongLianService;
import com.circle.interests.biz.core.protocol.RechargeResult;
import com.circle.interests.biz.entity.PrizeHonglianProduct;
import com.circle.interests.biz.enums.PrizeStatusEnum;
import com.circle.interests.biz.service.IPrizeHonglianProductService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 鸿联虚拟奖品
 *
 * @author zhou
 * @date 2021/12/8
 */
@Slf4j
@Component
public class PrizeHongLianHandler {

    @DubboReference
    private DubboHongLianService dubboHongLianService;
    @Resource
    private IPrizeHonglianProductService prizeHonglianProductService;

    public RechargeResult recharge(String orderCode, String phoneNumber, String productCode, String remark) {
        RechargeResult result = new RechargeResult();
        PrizeHonglianProduct prizeHonglianProduct = prizeHonglianProductService.getByProductCode(productCode);
        if (prizeHonglianProduct == null) {
            result.setPrizeStatus(PrizeStatusEnum.WRITE_OFF_FAIL.getCode());
            result.setStatusInfo("鸿联商品未找到");
            return result;
        }
        result.setUseType(prizeHonglianProduct.getUseType());
        boolean phoneFee = productCode.startsWith("HF");
        try {
            if (phoneFee) {
                if (StrUtil.isBlank(phoneNumber)) {
                    result.setPrizeStatus(PrizeStatusEnum.WRITE_OFF_FAIL.getCode());
                    result.setStatusInfo("手机号不能为空");
                } else {
                    String orderId = dubboHongLianService.phoneFeeTopUp(orderCode, phoneNumber, prizeHonglianProduct.getProductPrice().intValue());
                    log.info("请求鸿联话费参数:{},{},{},结果:{}", phoneNumber, productCode, prizeHonglianProduct.getUseType(), orderId);
                    result.setPrizeStatus(PrizeStatusEnum.WRITE_OFF_SUCCESS.getCode());
                    result.setOrderCode(orderId);
                }
            } else {
                HongLianOrderDto hongLianOrderDto = dubboHongLianService.prizeTopUp(orderCode, productCode,
                        phoneNumber, EnumHongLianVirtualPrizeType.enumHongLianPrizeType(prizeHonglianProduct.getUseType()), remark);
                log.info("请求鸿联参数:{},{},{},结果:{}", phoneNumber, productCode, prizeHonglianProduct.getUseType(), JSONUtil.toJsonStr(hongLianOrderDto));
                if (StrUtil.isNotBlank(hongLianOrderDto.getCardNo()) && StrUtil.isNotBlank(hongLianOrderDto.getCardPassword())) {
                    result.setPrizeStatus(PrizeStatusEnum.WRITE_OFF_SUCCESS.getCode());
                    result.setCardNo(hongLianOrderDto.getCardNo());
                    result.setCardPassword(hongLianOrderDto.getCardPassword());
                } else {
                    result.setPrizeStatus(PrizeStatusEnum.WRITE_OFF_ING.getCode());
                }
                result.setOrderCode(hongLianOrderDto.getOrderCode());
                result.setStatusInfo(hongLianOrderDto.getStatusInfo());
            }
        } catch (Exception e) {
            result.setPrizeStatus(PrizeStatusEnum.WRITE_OFF_FAIL.getCode());
            log.info("请求鸿联接口失败:" + e.getMessage(), e);
        }
        return result;
    }


}
